#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <archive.h>
#include <archive_entry.h>

#define BOLD_GRAY "\033[1;90m"
#define RED "\033[0;31m"
#define WHITE "\033[0;37m"
#define RESET "\033[0m"

int module_07_run(const char *filename) {
    char log_msg[1024];

    struct archive *a = archive_read_new();
    if (!a) {
        snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " RED "Error initializing libarchive" RESET);
        printf("%s\n", log_msg);
        return 1;
    }

    archive_read_support_format_all(a);
    archive_read_support_filter_all(a);

    if (archive_read_open_filename(a, filename, 10240) != ARCHIVE_OK) {
        snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " RED "Not an archive: %s" RESET,
                 archive_error_string(a));
        printf("%s\n", log_msg);
        archive_read_free(a);
        return 1;
    }

    struct archive_entry *entry;
    int entries_read = 0;
    while (archive_read_next_header(a, &entry) == ARCHIVE_OK) {
        entries_read++;
    }

    if (entries_read > 0) {
        snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " WHITE "Archive format: %s, Entries: %d" RESET,
                 archive_format_name(a), entries_read);
    } else {
        snprintf(log_msg, sizeof(log_msg), BOLD_GRAY " Library libarchive: " RED "Corrupted or empty archive" RESET);
    }
    printf("%s\n", log_msg);
    archive_read_close(a);
    archive_read_free(a);
    return 0;
}
